﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;


namespace AladdinGameFramework.UGUI
{
	[DisallowMultipleComponent]
	public class CUIWarpContent : MonoBehaviour
	{
		public delegate void OnInitializeItem(GameObject go, int dataIndex);
		public enum Arrangement
		{
			Horizontal,
			Vertical
		}
		public CUIWarpContent.OnInitializeItem onInitializeItem;
		public CUIWarpContent.Arrangement arrangement = CUIWarpContent.Arrangement.Horizontal;
		[Range(1f, 50f)]
		public int maxPerLine = 1;
		public float cellWidth = 200f;
		public float cellHeight = 200f;
		[Range(0f, 50f)]
		public float cellWidthSpace = 0f;
		[Range(0f, 50f)]
		public float cellHeightSpace = 0f;
		[Range(0f, 30f)]
		public int viewCount = 5;
		public ScrollRect scrollRect;
		public RectTransform content;
		private int dataCount;
		private int curScrollPerLineIndex = -1;
		private List<CUIWarpContentItem> listItem;
		private Queue<CUIWarpContentItem> unUseItem;
		private void Awake()
		{
			this.listItem = new List<CUIWarpContentItem>();
			this.unUseItem = new Queue<CUIWarpContentItem>();
		}
		public void Init(int dataCount)
		{
			bool flag = this.scrollRect == null || this.content == null;
			if (flag)
			{
				Debug.LogError(string.Concat(new object[]
				{
					"异常:请检测<",
					base.gameObject.name,
					">对象上UIWarpContent对应ScrollRect、Content 是否存在值....",
					this.scrollRect,
					" _",
					this.content
				}));
			}
			else
			{
				bool flag2 = dataCount < 0;
				if (!flag2)
				{
					this.setDataCount(dataCount);
					this.scrollRect.onValueChanged.RemoveAllListeners();
					this.scrollRect.onValueChanged.AddListener(new UnityAction<Vector2>(this.onValueChanged));
					this.Clear();
					this.unUseItem.Clear();
					this.listItem.Clear();
					this.setUpdateRectItem(0);
				}
			}
		}
		private void setDataCount(int count)
		{
			bool flag = this.dataCount == count;
			if (!flag)
			{
				this.dataCount = count;
				this.setUpdateContentSize();
			}
		}
		private void onValueChanged(Vector2 vt2)
		{
			CUIWarpContent.Arrangement arrangement = this.arrangement;
			if (arrangement != CUIWarpContent.Arrangement.Horizontal)
			{
				if (arrangement == CUIWarpContent.Arrangement.Vertical)
				{
					float y = vt2.y;
					bool flag = y >= 1f || y <= 0f;
					if (flag)
					{
						return;
					}
				}
			}
			else
			{
				float x = vt2.x;
				bool flag2 = x <= 0f || x >= 1f;
				if (flag2)
				{
					return;
				}
			}
			int num = this.getCurScrollPerLineIndex();
			bool flag3 = num == this.curScrollPerLineIndex;
			if (!flag3)
			{
				this.setUpdateRectItem(num);
			}
		}
		private void setUpdateRectItem(int scrollPerLineIndex)
		{
			bool flag = scrollPerLineIndex < 0;
			if (!flag)
			{
				this.curScrollPerLineIndex = scrollPerLineIndex;
				int num = this.curScrollPerLineIndex * this.maxPerLine;
				int num2 = (this.curScrollPerLineIndex + this.viewCount) * this.maxPerLine;
				for (int num3 = this.listItem.Count - 1; num3 >= 0; num3--)
				{
					CUIWarpContentItem cUIWarpContentItem = this.listItem[num3];
					int index = cUIWarpContentItem.Index;
					bool flag2 = index < num || index >= num2;
					if (flag2)
					{
						cUIWarpContentItem.Index = -1;
						this.listItem.Remove(cUIWarpContentItem);
						this.unUseItem.Enqueue(cUIWarpContentItem);
					}
				}
				for (int i = num; i < num2; i++)
				{
					bool flag3 = i >= this.dataCount;
					if (!flag3)
					{
						bool flag4 = this.isExistDataByDataIndex(i);
						if (!flag4)
						{
							this.createItem(i);
						}
					}
				}
			}
		}
		public void AddItem(int dataIndex)
		{
			bool flag = dataIndex < 0 || dataIndex > this.dataCount;
			if (!flag)
			{
				bool flag2 = false;
				for (int num = this.listItem.Count - 1; num >= 0; num--)
				{
					CUIWarpContentItem cUIWarpContentItem = this.listItem[num];
					bool flag3 = cUIWarpContentItem.Index >= this.dataCount - 1;
					if (flag3)
					{
						flag2 = true;
						break;
					}
				}
				this.setDataCount(this.dataCount + 1);
				bool flag4 = flag2;
				if (flag4)
				{
					for (int i = 0; i < this.listItem.Count; i++)
					{
						CUIWarpContentItem cUIWarpContentItem2 = this.listItem[i];
						int index = cUIWarpContentItem2.Index;
						bool flag5 = index >= dataIndex;
						if (flag5)
						{
							cUIWarpContentItem2.Index = index + 1;
						}
					}
					this.setUpdateRectItem(this.getCurScrollPerLineIndex());
				}
				else
				{
					for (int j = 0; j < this.listItem.Count; j++)
					{
						CUIWarpContentItem cUIWarpContentItem3 = this.listItem[j];
						int index2 = cUIWarpContentItem3.Index;
						bool flag6 = index2 >= dataIndex;
						if (flag6)
						{
							cUIWarpContentItem3.Index = index2;
						}
					}
				}
			}
		}
		public void DelItem(int dataIndex)
		{
			bool flag = dataIndex < 0 || dataIndex >= this.dataCount;
			if (!flag)
			{
				bool flag2 = this.listItem.Count >= this.dataCount;
				this.setDataCount(this.dataCount - 1);
				for (int num = this.listItem.Count - 1; num >= 0; num--)
				{
					CUIWarpContentItem cUIWarpContentItem = this.listItem[num];
					int index = cUIWarpContentItem.Index;
					bool flag3 = index == dataIndex;
					if (flag3)
					{
						this.listItem.Remove(cUIWarpContentItem);
						bool flag4 = flag2;
						if (flag4)
						{
							UnityEngine.Object.Destroy(cUIWarpContentItem.gameObject);
						}
						else
						{
							cUIWarpContentItem.Index = -1;
							this.unUseItem.Enqueue(cUIWarpContentItem);
						}
					}
					bool flag5 = index > dataIndex;
					if (flag5)
					{
						cUIWarpContentItem.Index = index - 1;
					}
				}
				this.setUpdateRectItem(this.getCurScrollPerLineIndex());
			}
		}
		public Vector3 getLocalPositionByIndex(int index)
		{
			float x = 0f;
			float y = 0f;
			float z = 0f;
			CUIWarpContent.Arrangement arrangement = this.arrangement;
			if (arrangement != CUIWarpContent.Arrangement.Horizontal)
			{
				if (arrangement == CUIWarpContent.Arrangement.Vertical)
				{
					x = (float)(index % this.maxPerLine) * (this.cellWidth + this.cellWidthSpace);
					y = (float)(-(float)(index / this.maxPerLine)) * (this.cellHeight + this.cellHeightSpace);
				}
			}
			else
			{
				x = (float)(index / this.maxPerLine) * (this.cellWidth + this.cellWidthSpace);
				y = (float)(-(float)(index % this.maxPerLine)) * (this.cellHeight + this.cellHeightSpace);
			}
			return new Vector3(x, y, z);
		}
		public void BackToTop()
		{
			this.content.anchoredPosition = Vector2.zero;
		}
		public void Clear()
		{
			while (this.listItem.Count > 0)
			{
				UnityEngine.Object.DestroyImmediate(this.listItem[0].gameObject);
				this.listItem.RemoveAt(0);
			}
			while (this.unUseItem.Count > 0)
			{
				CUIWarpContentItem cUIWarpContentItem = this.unUseItem.Dequeue();
				UnityEngine.Object.DestroyImmediate(cUIWarpContentItem.gameObject);
			}
			this.BackToTop();
		}
		private void createItem(int dataIndex)
		{
			bool flag = this.unUseItem.Count > 0;
			CUIWarpContentItem cUIWarpContentItem;
			if (flag)
			{
				cUIWarpContentItem = this.unUseItem.Dequeue();
			}
			else
			{
				GameObject go = new GameObject();
				cUIWarpContentItem = this.addChild(go, this.content).AddComponent<CUIWarpContentItem>();
			}
			cUIWarpContentItem.WarpContent = this;
			cUIWarpContentItem.Index = dataIndex;
			this.listItem.Add(cUIWarpContentItem);
		}
		private bool isExistDataByDataIndex(int dataIndex)
		{
			bool flag = this.listItem == null || this.listItem.Count <= 0;
			bool result;
			if (flag)
			{
				result = false;
			}
			else
			{
				for (int i = 0; i < this.listItem.Count; i++)
				{
					bool flag2 = this.listItem[i].Index == dataIndex;
					if (flag2)
					{
						result = true;
						return result;
					}
				}
				result = false;
			}
			return result;
		}
		private int getCurScrollPerLineIndex()
		{
			CUIWarpContent.Arrangement arrangement = this.arrangement;
			int result;
			if (arrangement != CUIWarpContent.Arrangement.Horizontal)
			{
				if (arrangement != CUIWarpContent.Arrangement.Vertical)
				{
					result = 0;
				}
				else
				{
					result = Mathf.FloorToInt(Mathf.Abs(this.content.anchoredPosition.y) / (this.cellHeight + this.cellHeightSpace));
				}
			}
			else
			{
				result = Mathf.FloorToInt(Mathf.Abs(this.content.anchoredPosition.x) / (this.cellWidth + this.cellWidthSpace));
			}
			return result;
		}
		private void setUpdateContentSize()
		{
			int num = Mathf.CeilToInt((float)this.dataCount / (float)this.maxPerLine);
			CUIWarpContent.Arrangement arrangement = this.arrangement;
			if (arrangement != CUIWarpContent.Arrangement.Horizontal)
			{
				if (arrangement == CUIWarpContent.Arrangement.Vertical)
				{
					this.content.sizeDelta = new Vector2(this.content.sizeDelta.x, this.cellHeight * (float)num + this.cellHeightSpace * (float)(num - 1));
				}
			}
			else
			{
				this.content.sizeDelta = new Vector2(this.cellWidth * (float)num + this.cellWidthSpace * (float)(num - 1), this.content.sizeDelta.y);
			}
		}
		private GameObject addChild(GameObject go, Transform parent)
		{
			bool flag = go == null || parent == null;
			GameObject result;
			if (flag)
			{
				Debug.LogError("异常。UIWarpContent.cs addChild(goPrefab = null  || parent = null)");
				result = null;
			}
			else
			{
				RectTransform rectTransform = go.AddComponent<RectTransform>();
				rectTransform.sizeDelta = new Vector2(this.cellWidth, this.cellHeight);
				rectTransform.anchorMin = new Vector2(0f, 1f);
				rectTransform.anchorMax = new Vector2(0f, 1f);
				rectTransform.pivot = new Vector2(0f, 1f);
				go.layer = parent.gameObject.layer;
				go.transform.SetParent(parent, false);
				result = go;
			}
			return result;
		}
		private void OnDestroy()
		{
			this.scrollRect = null;
			this.content = null;
			this.onInitializeItem = null;
			this.listItem.Clear();
			this.unUseItem.Clear();
			this.listItem = null;
			this.unUseItem = null;
		}
	}
}
